class Solution(object):
    def sumOfBeauties(self, nums):
        n = len(nums)
        pre = [0] * n
        suff = [0] * n
        pre[0] = nums[0]
        suff[n - 1] = nums[n - 1]
        ans = 0
        for i in range(1, n):
            pre[i] = max(nums[i], pre[i - 1])
            suff[n - 1 - i] = min(nums[n - 1 - i], suff[n - i])
        for i in range(1, n - 1):
            if pre[i - 1] < nums[i] < suff[i + 1]:
                ans += 2
            else:
                if nums[i - 1] < nums[i] < nums[i + 1]:
                    ans += 1
        return ans